/**
 * Created by waterworld on 17-1-16.
 */
var ots = require('./../../ots2');
var co=require('co');
var P = require("bluebird");
var Evt = require('events').EventEmitter;
var util = require('util');

var otsClass = module.exports;

otsClass.init=function()
{
    this.client = ots.createClient({
        accessKeyID: 'LTAIFdXNv9QLU5cc',
        accessKeySecret: 'vnpfJQ7zwT0Yrwd3Trk7jtCQMjZPMQ',
        instance: 'snstable',
        region: 'cn-hangzhou',
        keepAliveMsecs: 1000, // default 1000
    });

}
otsClass.init();
otsClass.createTable=P.coroutine(function*(tableName,keys)
{
   // var keys = [{ 'name': 'uid', 'type': 'STRING' },{ 'name': 'uid1', 'type': 'INTEGER' }];
    //var capacityUnit = {read: 1, write: 0};
    //var response = yield client.createTable('test1', keys, capacityUnit);
    var self=this;
    var    Ticker=function(){};
    util.inherits(Ticker,Evt);
    var ticker = new Ticker();
    var ended = new Promise(function(resolve, reject) {ticker.on("tick", resolve);});
    function* otsAsync()
    {
       try{
            var capacityUnit = {read: 1, write: 0};
            var response = yield self.client.createTable(tableName, keys, capacityUnit);
            ticker.emit('tick',response);
        }
        catch(e)
        {ticker.emit('tick',-1);
        }

    }

//workAsync();
    co(otsAsync);
    var ret=-1;
    yield ended.then(function(num){ ret=num });
    return ret;
});
//OTSObjectNotExistError
otsClass.getTableInfo=P.coroutine(function*(tableName)
{
    var self=this;
    var    Ticker=function(){};
    util.inherits(Ticker,Evt);
    var ticker = new Ticker();
    var ended = new Promise(function(resolve, reject) {ticker.on("tick", resolve);});
    function* otsAsync()
    {
        try{var response = yield self.client.describeTable(tableName);
            ticker.emit('tick',response);
        }
        catch(e)
        {ticker.emit('tick',e);
        }

    }

//workAsync();
    co(otsAsync);
    var ret=-1;
    yield ended.then(function(res){
            ret=res;
    });
    return ret;
});


otsClass.insertRecord=P.coroutine(function*(tableName,keys,columns)
{
    var self=this;
    var condition = {
        row_existence: ots.RowExistenceExpectation.IGNORE
    };
   // var primaryKeys = {uid: 'test_uid'};
    //var columns = {test: 'test_value'};
   // var response = yield client.putRow(name, condition, primaryKeys, columns);
    var    Ticker=function(){};
    util.inherits(Ticker,Evt);
    var ticker = new Ticker();
    var ended = new Promise(function(resolve, reject) {ticker.on("tick", resolve);});
    function* otsAsync()
    {
        try{
            var response = yield self.client.putRow(tableName, condition, keys, columns);
            ticker.emit('tick',response);
        }
        catch(e)
        {ticker.emit('tick',-1);
        }

    };
    co(otsAsync);
    var ret=-1;
    yield ended.then(function(res){ ret=res });
    return ret;
});

otsClass.readRecord=P.coroutine(function*(tableName,keys,columns)
{
    var self=this;
    var condition = {
        row_existence: ots.RowExistenceExpectation.IGNORE
    };
    // var primaryKeys = {uid: 'test_uid'};
    //var columns = ['test'];

    var    Ticker=function(){};
    util.inherits(Ticker,Evt);
    var ticker = new Ticker();
    var ended = new Promise(function(resolve, reject) {ticker.on("tick", resolve);});
    function* otsAsync()
    {
        try{
            var response = yield self.client.getRow(tableName,keys,columns);
            response.ret=1;
            ticker.emit('tick',response);
        }
        catch(e)
        {   e.ret=-1;
            ticker.emit('tick',e);
        }

    };
    co(otsAsync);
    var ret=-1;
    yield ended.then(function(res){ ret=res });
    return ret;
});


otsClass.updateRecord=P.coroutine(function*(tableName,keys,columns)
{
    var self=this;
    var condition = {
        row_existence: ots.RowExistenceExpectation.IGNORE
    };
    // var primaryKeys = {uid: 'test_uid'};
    //var columns = {test: 'test_value'};
    // var response = yield client.putRow(name, condition, primaryKeys, columns);
    var    Ticker=function(){};
    util.inherits(Ticker,Evt);
    var ticker = new Ticker();
    var ended = new Promise(function(resolve, reject) {ticker.on("tick", resolve);});
    function* otsAsync()
    {
        try{
            var response = yield self.client.updateRow(tableName, condition, keys, columns);
            response.ret=1;
            ticker.emit('tick',response);
        }
        catch(e)
        {   e.ret=-1;
            ticker.emit('tick',e);
        }

    };
    co(otsAsync);
    var ret=-1;
    yield ended.then(function(res){ ret=res });
    return ret;
});
